package bytezero.threadsynchronization;



/**
 * 使用同步方法解决实现 继承 Thread类 的线程安全问题
 *
 *
 *
 * @author Bytezero1·zhenglei!    Email:420498246@qq.com
 * create  2021-10-17 16:38
 */
 class WindowM extends Thread{
    private static int ticket = 100;

    @Override
    public void run() {
        while (true){

            show();
        }
    }

    private static synchronized void show(){  //同步监视器：WindowM.class
   // private synchronized void show(){   //同步监视器： w1 w2 w3 此种解决方式是错误的
        if(ticket > 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":卖票，票号为："+ticket);
            ticket--;
        }
    }

}


public class WindowMethod {
    public static void main(String[] args) {
        WindowM w1 = new WindowM();
        WindowM w2 = new WindowM();
        WindowM w3 = new WindowM();

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");

        w1.start();
        w2.start();
        w3.start();

    }

}


